package com.hhxy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.hhxy.model.dto.UserLoginDto;
import com.hhxy.model.entity.User;
import com.hhxy.model.vo.AdminUserInfoVo;
import com.hhxy.model.vo.RoutersVo;
import com.hhxy.model.vo.UserInfoVo;
import com.hhxy.service.LoginService;
import com.hhxy.service.MenuService;
import com.hhxy.service.RoleService;
import com.hhxy.utils.BeanCopyUtils;
import com.hhxy.utils.SecurityUtils;
import com.hhxy.utils.response.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ghp
 * @date 2023/4/29
 * @title
 * @description
 */
@Api(tags = {"后端接口", "登录注册"})
@ApiSupport(author = "ghp", order = 1)
@RestController
@RequestMapping
public class BeLoginController {

    @Resource
    private LoginService loginService;

    @Resource
    private MenuService menuService;

    @Resource
    private RoleService roleService;

    @PostMapping("/user/login")
    @ApiOperation(value = "用户登录", notes = "用户名和密码不能为空")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult login(@RequestBody @Validated UserLoginDto userLoginDto){
        return loginService.login(userLoginDto);
    }

    @GetMapping("/getInfo")
    @ApiOperation(value = "获取角色信息和对应的权限信息", notes = "登录后被自动调用")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult getInfo(){
        // 根据用户id查询权限信息
        List<String> perms = menuService.selectPermsByUserId();
        // 根据用户id查询角色信息
        List<String> roleKeyList = roleService.selectRoleKeyByUserId();
        // 封装VO，然后响应
        User user = SecurityUtils.getLoginUser().getUser();
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVo.class);
        AdminUserInfoVo adminUserInfoVo = new AdminUserInfoVo(perms,roleKeyList,userInfoVo);
        return ResponseResult.okResult(adminUserInfoVo);
    }

    @GetMapping("/getRouters")
    @ApiOperation(value = "获取路由信息", notes = "登录后被自动调用")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult<RoutersVo> getRouters(){
        return menuService.selectRouterMenuTreeByUserId();
    }

    @PostMapping("/user/logout")
    @ApiOperation(value = "用户退出")
    @ApiOperationSupport(author = "ghp", order = 1)
    public ResponseResult logout(){
        return loginService.logout();
    }

}
